/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tpdisenio2009.services;

import java.util.LinkedList;
import java.util.List;
import org.hibernate.HibernateException;
import tpdisenio2009.model.beans.Administrator;
import tpdisenio2009.model.beans.City;
import tpdisenio2009.model.beans.Country;
import tpdisenio2009.model.beans.Journalist;
import tpdisenio2009.model.beans.Person;
import tpdisenio2009.model.beans.State;
import tpdisenio2009.model.beans.User;
import tpdisenio2009.model.dao.AdministratorDAO;
import tpdisenio2009.model.dao.CityDAO;
import tpdisenio2009.model.dao.CountryDAO;
import tpdisenio2009.model.dao.JournalistDAO;
import tpdisenio2009.model.dao.StateDAO;
import tpdisenio2009.model.dao.UserDAO;

/**
 *
 * @author DeuterioX
 */
public class UserService {

    private final UserDAO userDAO;

    private final CountryDAO countryDAO;

    private final CityDAO cityDAO;

    private final StateDAO stateDAO;

    private final JournalistDAO journalistDAO;

    private final AdministratorDAO administratorDAO;

    public UserService() {
        userDAO = new UserDAO();
        countryDAO = new CountryDAO();
        stateDAO = new StateDAO();
        cityDAO = new CityDAO();
        journalistDAO = new JournalistDAO();
        administratorDAO = new AdministratorDAO();
    }

    public int countUsers() {
        return userDAO.count();
    }

    public List<Country> getCountry() {
        return countryDAO.getAll();
    }

    public List<State> getState( Country country ) {
        return stateDAO.getByCountry( country );
    }

    public List<City> getCity( State state ) {
        return cityDAO.getByState( state );
    }

    public void createUser( User user ) throws HibernateException {
        try {
            userDAO.insert( user );
        } catch (HibernateException ex) {
            throw ex;
        }
    }

    public boolean userExist( String document, String documentType ) {
        User user = userDAO.getUserByDocument( document, documentType );
        return ( user != null )? true : false;
    }

    public LinkedList<String> getDocumentType() {
        LinkedList<String> dt = new LinkedList<String>();
        dt.add("DNI");
        dt.add("LC");
        dt.add("LE");
        return dt;
    }

    public LinkedList<Character> getGenre() {
        LinkedList<Character> genre = new LinkedList<Character>();
        genre.add( 'M' );
        genre.add( 'F' );
        return genre;
    }

    public Person getPersonByDocument( String document, String documentType ) {
        User user = userDAO.getUserByDocument( document, documentType );
        if (user != null)
            return user;

        Journalist journalist = journalistDAO.getJournalistByDocument( document, documentType );
        if (journalist != null)
            return journalist;

        Administrator administrator = administratorDAO.getAdminByDocument( document, documentType );
        if (administrator != null)
            return administrator;

        return null;
    }

    public void update( User user ) {
        userDAO.update( user );
    }

}
